package com.dolphin.fxc.auth.work.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dolphin.fxc.auth.work.mapper.SysUserMapper;
import com.dolphin.fxc.auth.work.pojo.entity.SysUser;
import com.dolphin.fxc.auth.work.pojo.entity.auth.JwtUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 权限认证器查询数据库用户名密码 与 前端传的进行对比
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserMapper userMapper;
//    @Autowired
//    private RoleMapper roleMapper;
//    @Autowired
//    private MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String userCode) throws UsernameNotFoundException {
        SysUser userEntity = userMapper.selectOne(Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getUserCode, userCode).eq(SysUser::getDelFlag, 0));
        if (Objects.isNull(userEntity)) {
            return null;
        }
//        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
//        fillUserAuthority(userEntity, authorities);
//        List<String> roles = authorities.stream()
//                .map(SimpleGrantedAuthority::getAuthority).collect(Collectors.toList());
//        JwtUserEntity jwtUserEntity = new JwtUserEntity(userEntity.getId(),
//                username,
//                userEntity.getPassword(),
//                null,
//                null);
        JwtUserEntity jwtUserEntity = new JwtUserEntity();
        jwtUserEntity.setId(userEntity.getId());
        jwtUserEntity.setUserCode(userEntity.getUserCode());
        jwtUserEntity.setUsername(userEntity.getUserName());
        jwtUserEntity.setPassword(userEntity.getPassword());
        return jwtUserEntity;
    }

//    private void fillUserAuthority(SysUser userEntity, List<SimpleGrantedAuthority> authorities) {
//        List<RoleEntity> roleEntities = roleMapper.findRoleByUserId(userEntity.getId());
//        if (CollectionUtils.isEmpty(roleEntities)) {
//            return;
//        }
//
//        Set<String> permissionSet = roleEntities.stream()
//                .filter(x -> StringUtils.hasLength(x.getPermission())).map(RoleEntity::getPermission)
//                .collect(Collectors.toSet());
//        fillRoleMenu(roleEntities, permissionSet);
//        if (CollectionUtils.isNotEmpty(permissionSet)) {
//            authorities.addAll(permissionSet.stream().map(x -> new SimpleGrantedAuthority(x)).collect(Collectors.toList()));
//        }
//    }
//
//    private void fillRoleMenu(List<RoleEntity> roleEntities, Set<String> permissionSet) {
//        List<Long> roleIdList = roleEntities.stream().map(RoleEntity::getId).collect(Collectors.toList());
//        List<MenuEntity> menuList = menuMapper.findMenuByRoleIdList(roleIdList);
//        if (CollectionUtils.isEmpty(menuList)) {
//            return;
//        }
//
//        for (MenuEntity menuEntity : menuList) {
//            if (StringUtils.hasLength(menuEntity.getPermission())) {
//                Set<String> menuPermissionSet = Arrays.stream(menuEntity.getPermission().split(",")).collect(Collectors.toSet());
//                permissionSet.addAll(menuPermissionSet);
//            }
//        }
//    }
}
